<?php
	$category_index = array(
		'title' =>
			'<div class="head">' .
				'<h1>' . 
				sprintf(
					__d('lil_cms', 'Menu items for "%s"'),
					$this->Html->clean($admin_area['Area']['name'])
				).
				'</h1>'.
			'</div>',
		'actions' => array(
			'pre' => '<div>',
			'post' => '</div>',
			'lines' => array(
			)
		),
		'menu' => array(
			'add' => array(
				'title' => __d('lil_cms', 'Add'),
				'visible' => true,
				'url'   => array(
					'admin'      => true,
					'plugin'     => 'lil_cms',
					'controller' => 'cms_menus',
					'action'     => 'add',
				)
			)
		),
		'table' => array(
			'pre' => '<div class="index">' . PHP_EOL,
			'post' => '</div>',
			'element' => array(
				'parameters' => array(
					'cellspacing' => 0,
					'cellpadding' => 0,
					'id'          => 'AdminMenusIndex',
					'width'       => '100%'
				),
			)
		)
	);

	$category_index['table']['element']['head'] = array(
		'parameters' => array(),
		'rows' => array(
			0 => array(
				'parameters' => array(),
				'columns' => array(
					'id' => array(
						'parameters' => array('class' => 'center'),
						'html'       => __d('lil_cms', 'ID'),
					),
					'name' => array(
						'parameters' => array('class' => 'left'),
						'html'       => __d('lil_cms', 'Name'),
					),
					'url' => array(
						'parameters' => array('class' => 'left'),
						'html'       => __d('lil_cms', 'Url'),
					),
					'edit' => array(
						'parameters' => array(),
						'html'       => '&nbsp;',
					),
					'delete' => array(
						'parameters' => array(),
						'html'       => '&nbsp;',
					),
					'up' => array(
						'parameters' => array(),
						'html'       => '&nbsp;',
					),
				)
			)
		)
	);
	
	// table body values
	$category_index['table']['element']['body']['rows'] = array();


	// start with an empty $right stack
	$i = 0; $right = array(); 
	foreach($data as $item) {
		if (empty($item['CmsMenu']['parent_id'])) $right = array();
		// only check stack if there is one
		if (count($right)>0) {
			// check if we should remove a node from the stack
			while ($right[count($right) - 1] < $item['CmsMenu']['rght']) {
				array_pop($right);
			}
		}
		
		// display indented node title
		$level = count($right);
		
		// add this node to the stack
		$right[] = $item['CmsMenu']['rght'];
		
		$category_index['table']['element']['body']['rows'][$i] = array(
			'post' => $item,
			'parameters' => ($i++ % 2 == 0) ? array() : array('class' => 'altrow'),
			'columns' => array(
				'id' => array(
					'parameters' => array('class' => 'center'),
					'html' => $item['CmsMenu']['id']
				),
				'name' => array(
					'parameters' => array(
						'class' => 'big' . (($level == 0) ? ' strong' : ''),
						'style' => 'padding-left: ' . ($level + 1) * 12 . 'px;'
					),
					'html' => h($item['CmsMenu']['caption'])
				),
				'url' => array(
					'parameters' => array(),
					'html' => h($item['CmsMenu']['url'])
				),
				'edit' => array(
					'parameters' => array('class' => 'center'),
					'html' => $this->Html->link(
						$this->Html->image(
							'/lil/img/edit.gif', 
							array('alt' => __d('lil_cms', 'Edit'))
						),
						array(
							'admin'  => true,
							'action' => 'edit',
							$item['CmsMenu']['id']
						),
						array(
							'title' => __d('lil_cms', 'Edit'),
							'escape' => false
						)
					)
				),
				'delete' => array(
					'parameters' => array('class' => 'center'),
					'html' => $this->Html->link(
						$this->Html->image('/lil/img/delete.gif',
							array('alt'=>__d('lil_cms', 'Delete'))
						),
						array(
							'admin'  => true,
							'action' => 'delete',
							$item['CmsMenu']['id']
						),
						array(
							'title' => __d('lil_cms', 'Delete'),
							'confirm' => __d('lil_cms', 'Are you sure you want to delete \'%s\'?', 
								$item['CmsMenu']['caption']),
							'escape' => false
						)
					)
				),
				'up' => array(
					'parameters' => array('class' => 'center'),
					'html' => 
						$this->Html->link(
							$this->Html->image('/lil/img/up.gif',
								array('alt'=>__d('lil_cms', 'Up'))
							),
							array(
								'admin'  => true,
								'action' => 'move_up',
								$item['CmsMenu']['id']
							),
							array(
								'title' => __d('lil_cms', 'Up'),
								'escape' => false
							)
						) . 
						$this->Html->link(
							$this->Html->image('/lil/img/down.gif',
								array('alt'=>__d('lil_cms', 'Down'))
							),
							array(
								'admin'  => true,
								'action' => 'move_down',
								$item['CmsMenu']['id']
							),
							array(
								'title' => __d('lil_cms', 'Down'),
								'escape' => false
							)
						)
				),
			)
		);
			
	}
	
	///////////////////////////////////////////////////////////////////////////////////////////////
	// call plugin handlers
	$category_index = $this->callPluginHandlers('lil_cms_admin_index_categories', $category_index);
	
	///////////////////////////////////////////////////////////////////////////////////////////////
	// form display begins
	$this->Lil->index($category_index);